from django.db import migrations


def add_bid_price_column(apps, schema_editor):
    connection = schema_editor.connection
    with connection.cursor() as cursor:
        try:
            cursor.execute("SHOW COLUMNS FROM deal_bid_candidate LIKE 'bid_price'")
            exists = cursor.fetchone()
        except Exception:
            exists = None
        if not exists:
            cursor.execute("ALTER TABLE deal_bid_candidate ADD COLUMN bid_price DECIMAL(15,2)")
            try:
                cursor.execute("CREATE INDEX deal_bid_ca_bid_pri_fix_idx ON deal_bid_candidate (bid_price)")
            except Exception:
                pass


def noop_reverse(apps, schema_editor):
    pass


class Migration(migrations.Migration):
    dependencies = [
        ('deal', '0003_bidevaluationresult_opening_record_and_more'),
    ]

    operations = [
        migrations.RunPython(add_bid_price_column, noop_reverse),
    ]